package com.molichuxing.gateway.bff.financial.convertor;

import com.molichuxing.gateway.bff.financial.entity.request.create.EsignAccountCreateVo;
import com.molichuxing.gateway.bff.financial.entity.request.create.EsignCompanyCreateVo;
import com.molichuxing.gateway.bff.financial.entity.request.modify.OfflineAuditModifyVo;
import com.molichuxing.gateway.bff.financial.entity.response.*;
import com.molichuxing.services.business.dto.request.create.EsignAccountBizCreateDto;
import com.molichuxing.services.business.dto.request.create.EsignCompanyBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.OfflineAuditBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.OrderScheduleOfflineBizModifyDto;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.infrastructure.dto.request.create.InvoiceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.OfflineAuditModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.property.*;
import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Convert {

    /*********************************财务转换开始*************************************************/

    public static ReceiptVo toReceiptVo(PaymentBizDto beanBizDto) {
        ReceiptVo beanVo = null;
        if (beanBizDto != null) {
            beanVo = new ReceiptVo();
            BeanUtils.copyProperties(beanBizDto, beanVo);
            beanVo.setBusinessType(beanBizDto.getBusinessType().getValue());
            beanVo.setBusinessTypeStr(beanBizDto.getBusinessType().getName());
            beanVo.setPayWay(beanBizDto.getPayWay().getValue());
            beanVo.setPayWayStr(beanBizDto.getPayWay().getName());
            beanVo.setPayType(beanBizDto.getCostType().getValue());
            beanVo.setPayTypeStr(beanBizDto.getCostType().getName());
            beanVo.setFeeTotal(beanBizDto.getPayAmount());
            if (PaymentCostTypeEnum.RENEGE.equals(beanBizDto.getCostType())) {
                beanVo.setRepaymentPeriod(beanBizDto.getRemark());
            }
        }
        return beanVo;
    }

    public static ReceiptDownloadVo toReceiptDownloadVo(PaymentDownloadBizDto bizDto) {
        if (bizDto == null) {
            return null;
        }
        ReceiptDownloadVo vo = new ReceiptDownloadVo();
        BeanUtils.copyProperties(bizDto, vo);
        vo.setPayWayStr(bizDto.getPayWay().getName());
        if (PaymentBusinessTypeEnum.SUBSCRIBE.equals(bizDto.getBusinessType())) {
            vo.setPeriods(0);
        } else {
            vo.setPeriods(bizDto.getScheduleId() == 0 ? 1 : null);
        }

        return vo;
    }

    public static List<OfflineInfoVo> toOfflineVoList(List<OfflineDto> offlineDtoList) {
        List<OfflineInfoVo> voList = new ArrayList<>();
        if (offlineDtoList != null && offlineDtoList.size() > 0) {
            for (OfflineDto offlineDto : offlineDtoList) {
                voList.add(toOfflineVo(offlineDto));
            }
        }
        return voList;
    }

    public static OfflineInfoVo toOfflineVo(OfflineDto offlineDto) {
        OfflineInfoVo offlineInfoVo = new OfflineInfoVo();
        if (null == offlineDto) {
            return offlineInfoVo;
        }

        BeanUtils.copyProperties(offlineDto, offlineInfoVo);
        return offlineInfoVo;
    }

    public static OfflineAuditModifyDto toOfflineAuditModifyDto(OfflineAuditModifyVo offlineAuditModifyVo) {
        OfflineAuditModifyDto offlineAuditModifyDto = new OfflineAuditModifyDto();
        if (null == offlineAuditModifyVo) {
            return offlineAuditModifyDto;
        }

        BeanUtils.copyProperties(offlineAuditModifyDto, offlineAuditModifyDto);
        return offlineAuditModifyDto;
    }

    public static OfflineAuditBizModifyDto toOfflineAuditBizModifyDto(OfflineAuditModifyVo offlineAuditModifyVo) {
        OfflineAuditBizModifyDto offlineAuditBizModifyDto = new OfflineAuditBizModifyDto();
        if (null == offlineAuditModifyVo) {
            return offlineAuditBizModifyDto;
        }

        BeanUtils.copyProperties(offlineAuditModifyVo, offlineAuditBizModifyDto);
        return offlineAuditBizModifyDto;
    }

    public static Map<Integer, OrderScheduleOfflineBizModifyDto> toOrderScheduleOfflineMap(Map<Integer, OrderScheduleOfflineBizDto> map) {
        Map<Integer, OrderScheduleOfflineBizModifyDto> modifyMap = new HashMap<>();
        if (null == map) {
            return modifyMap;
        }

        for (Map.Entry<Integer, OrderScheduleOfflineBizDto> entry : map.entrySet()) {
            Integer key = entry.getKey();
            if (null == key) {
                continue;
            }
            OrderScheduleOfflineBizDto orderScheduleOfflineBizDto = entry.getValue();
            modifyMap.put(key, toOrderScheduleOfflineBizModifyDto(orderScheduleOfflineBizDto));
        }
        return modifyMap;
    }

    public static OrderScheduleOfflineBizModifyDto toOrderScheduleOfflineBizModifyDto(OrderScheduleOfflineBizDto orderScheduleOfflineBizDto) {
        OrderScheduleOfflineBizModifyDto orderScheduleOfflineBizModifyDto = new OrderScheduleOfflineBizModifyDto();
        if (null == orderScheduleOfflineBizDto) {
            return orderScheduleOfflineBizModifyDto;
        }

        BeanUtils.copyProperties(orderScheduleOfflineBizDto, orderScheduleOfflineBizModifyDto);
        return orderScheduleOfflineBizModifyDto;
    }


    public static List<ContractVo> toContractVoList(List<ContractDto> contractDtoList, Map<Long, OrderBizDto> contractOrderInfoMap,
                                                    Map<Integer, UserDto> userMap) {
        List<ContractVo> voList = new ArrayList<>();
        if (contractDtoList != null && contractDtoList.size() > 0) {
            for (ContractDto contractDto : contractDtoList) {
                voList.add(toContractVo(contractDto, contractOrderInfoMap, userMap));
            }
        }
        return voList;
    }

    public static ContractVo toContractVo(ContractDto contractDto, Map<Long, OrderBizDto> contractOrderInfoMap,
                                          Map<Integer, UserDto> userMap) {
        ContractVo contractVo = new ContractVo();
        if (null == contractDto) {
            return contractVo;
        }

        Long orderCode = contractDto.getOrderCode();
        BeanUtils.copyProperties(contractDto, contractVo);
        if (null == orderCode || null == contractOrderInfoMap || contractOrderInfoMap.size() < 1) {
            return contractVo;
        }

        OrderBizDto orderBizDto = contractOrderInfoMap.get(orderCode);
        if (null == orderBizDto) {
            return contractVo;
        }
        Integer userId = orderBizDto.getUserId();
        if (userId != null && userMap != null && userMap.size() > 0) {
            UserDto userDto = userMap.get(userId);
            if (userDto != null) {
                String realName = userDto.getRealName();
                contractVo.setRealName(realName);
            }
        }
        OrderTypeEnum orderTypeEnum = orderBizDto.getType();
        if (orderTypeEnum != null) {
            int value = orderTypeEnum.getValue();
            String name = orderTypeEnum.getName();
            contractVo.setBusinessType(value);
            contractVo.setBusinessTypeStr(name);
        }
        return contractVo;
    }


    public static List<InvoiceInfoVo> toInvoiceInfoVoList(List<InvoiceInfoBizDto> dtoList,
                                                          Map<Integer, UserDto> userMap) {
        List<InvoiceInfoVo> voList = new ArrayList<>();
        if (dtoList != null && dtoList.size() > 0) {
            for (InvoiceInfoBizDto invoiceInfoBizDto : dtoList) {
                voList.add(toInvoiceInfoVo(invoiceInfoBizDto, userMap));
            }
        }
        return voList;
    }

    public static InvoiceInfoVo toInvoiceInfoVo(InvoiceInfoBizDto invoiceInfoBizDto,
                                                Map<Integer, UserDto> userMap) {
        InvoiceInfoVo invoiceInfoVo = new InvoiceInfoVo();
        if (null == invoiceInfoBizDto) {
            return invoiceInfoVo;
        }
        BeanUtils.copyProperties(invoiceInfoBizDto, invoiceInfoVo);
        Integer userId = invoiceInfoBizDto.getUserId();
        if (userId != null && userMap != null && userMap.size() > 0) {
            UserDto userDto = userMap.get(userId);
            if (userDto != null) {
                String realName = userDto.getRealName();
                String phone = userDto.getPhone();

                invoiceInfoVo.setRealName(realName);
                invoiceInfoVo.setPhone(phone);
            }
        }

        return invoiceInfoVo;
    }

    public static List<EsignCompanyBizCreateDto> toEsignCompanyBizCreateDtoList(List<EsignCompanyCreateVo> voList) {
        List<EsignCompanyBizCreateDto> dtoList = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            for (EsignCompanyCreateVo esignCreateVo : voList) {
                dtoList.add(toEsignCompanyBizCreateDto(esignCreateVo));
            }
        }
        return dtoList;
    }

    public static EsignCompanyBizCreateDto toEsignCompanyBizCreateDto(EsignCompanyCreateVo esignCreateVo) {
        if (null == esignCreateVo) {
            return null;
        }

        EsignCompanyBizCreateDto esignBizCreateDto = new EsignCompanyBizCreateDto();
        BeanUtils.copyProperties(esignCreateVo, esignBizCreateDto);
        return esignBizCreateDto;
    }

    public static List<EsignCompanyBizCreateDto> toEsignCompanyBizCreateList(List<SubcompanyDto> voList) {
        List<EsignCompanyBizCreateDto> dtoList = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            for (SubcompanyDto esignCreateVo : voList) {
                dtoList.add(toEsignCompanyBizCreate(esignCreateVo));
            }
        }
        return dtoList;
    }

    public static EsignCompanyBizCreateDto toEsignCompanyBizCreate(SubcompanyDto dto) {
        if (null == dto) {
            return null;
        }

        EsignCompanyBizCreateDto esignBizCreateDto = new EsignCompanyBizCreateDto();
        BeanUtils.copyProperties(dto, esignBizCreateDto);
        esignBizCreateDto.setCompanyId(dto.getId());
        esignBizCreateDto.setType(EsignCompanyTypeEnum.ASSET_COMPANY.getValue());
        return esignBizCreateDto;
    }


    public static InvoiceCreateDto toInvoiceCreateDto(OfflineDto offlineDto, OrderExclusivelDto orderExclusivel,
                                                      OrderScheduleDto orderSchedule, DealerDetailDto dealerDetail) {
        InvoiceCreateDto createDto = new InvoiceCreateDto();
        // 服务费
        BigDecimal serviceFee = orderExclusivel.getServiceFee().divide(new BigDecimal(orderSchedule.getTotalPeriods()),
                0, RoundingMode.HALF_DOWN);
        if (orderSchedule.getTotalPeriods() == orderSchedule.getPeriods()) {
            // 最后一期
            serviceFee = orderExclusivel.getServiceFee()
                    .subtract(serviceFee.multiply(new BigDecimal(orderSchedule.getTotalPeriods() - 1)));
        }

        // 订单号
        createDto.setOrderCode(orderExclusivel.getOrderCode());
        // 下单时间
        createDto.setOrderTime(orderExclusivel.getOrderTime());
        // 支付时间/续订单生效的时
        createDto.setPayTime(offlineDto.getPayTime());
        // 支付金额
        createDto.setAmount(orderSchedule.getPayable());
        // 服务费
        createDto.setServiceFee(serviceFee);
        // 业务类型（1：订阅里程，2：专属里程）
        createDto.setBusinessType(InvoiceBusinessTypeEnum.EXCLUSIVEL.getValue());
        // 发票类型（11：主订单，12：续订单，13：加购，14：超时，15：超程，16：质损,21：分期，22：全款）
        createDto.setType(OrderPaymentMethodEnum.INSTALLMENT.equals(orderExclusivel.getPaymentMethod())
                ? InvoiceTypeEnum.INSTALLMENT.getValue() : InvoiceTypeEnum.FULL_PAYMENT.getValue());
        // 当前期数
        createDto.setCurrentPeriods(orderSchedule == null ? 1 : orderSchedule.getPeriods());
        // 总期数
        createDto.setTotalPeriods(
                orderSchedule == null ? orderExclusivel.getTransportTime() : orderSchedule.getTotalPeriods());
        // 下单门店
        createDto.setStoreId(orderExclusivel.getPlaceStoreId());
        // 用户id
        createDto.setUserId(orderExclusivel.getUserId());
        // 用车时长
        createDto.setTransportTime(orderExclusivel.getTransportTime());
        // 所属分子公司
        createDto.setSubcompanyId(dealerDetail.getSubcompanyId());

        return createDto;
    }

    public static List<EsignAccountBizCreateDto> toEsignAccountBizCreateDtoList(List<EsignAccountCreateVo> voList) {
        List<EsignAccountBizCreateDto> dtoList = new ArrayList<>();
        if (voList != null && voList.size() > 0) {
            for (EsignAccountCreateVo esignCreateVo : voList) {
                dtoList.add(toEsignAccountBizCreateDto(esignCreateVo));
            }
        }
        return dtoList;
    }

    public static EsignAccountBizCreateDto toEsignAccountBizCreateDto(EsignAccountCreateVo esignCreateVo) {
        if (null == esignCreateVo) {
            return null;
        }

        EsignAccountBizCreateDto esignBizCreateDto = new EsignAccountBizCreateDto();
        BeanUtils.copyProperties(esignCreateVo, esignBizCreateDto);
        return esignBizCreateDto;
    }

    /*********************************财务转换开始*************************************************/
}
